﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AfterlifeLib.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AfterlifeLib.GameScreens.Menus
{
    public class SelectedOptionChangedEventArgs : EventArgs
    {
        public string SelectedOption { get; set; }

        public SelectedOptionChangedEventArgs(string selectedOption)
        {
            SelectedOption = selectedOption;
        }
    }

    public class ListMenuEntry : MenuEntry
    {
        int selectedOptionIndex = 0;
        string[] options;

        public event EventHandler<SelectedOptionChangedEventArgs> SelectedOptionChanged;

        public ListMenuEntry(string text, string[] options)
            : base(text)
        {
            this.options = options;
        }

        public override void HandleInput(MenuScreen screen)
        {
            if (InputState.Left)
            {
                selectedOptionIndex--;

                if (selectedOptionIndex < 0)
                    selectedOptionIndex = options.Length - 1;
            }
            else if (InputState.Right)
            {
                selectedOptionIndex++;

                if (selectedOptionIndex > options.Length - 1)
                    selectedOptionIndex = 0;
            }
        }

        public override void Update(MenuScreen screen, bool isSelected, GameTime gameTime)
        {
            base.Update(screen, isSelected, gameTime);
        }

        public override void Draw(MenuScreen screen, Vector2 position, bool isSelected, GameTime gameTime)
        {
            //Welke kleur de MenuEntry moet zijn
            Color color = isSelected ? SelectedColor : NormalColor;
            color.A = screen.TransitionAlpha;

            SpriteBatch spriteBatch = screen.ScreenManager.SpriteBatch;
            SpriteFont font = screen.ScreenManager.Font;

            Vector2 origin = new Vector2(0, font.LineSpacing / 2);

            //Teken de text op het scherm
            spriteBatch.DrawString(font, Text, position, color, 0,
                                   origin, 1f, SpriteEffects.None, 0);

            string value = "< " + options[selectedOptionIndex] + " >";
            spriteBatch.DrawString(font, value, position + ValueOffset, color, 0,
                                   origin, 1f, SpriteEffects.None, 0);
        }

        void OnSelectedOptionChanged()
        {
            if (SelectedOptionChanged != null)
                SelectedOptionChanged(this, new SelectedOptionChangedEventArgs(options[selectedOptionIndex]));
        }
    }
}
